<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        v-loading="formLoading"
    >
                  <el-form-item label="用户编号" prop="userId">
                    <el-input v-model="formData.userId" placeholder="请输入用户编号"/>
                  </el-form-item>
                  <el-form-item label="联系人手机号" prop="contactUserMobile">
                    <el-input v-model="formData.contactUserMobile" placeholder="请输入联系人手机号"/>
                  </el-form-item>
                  <el-form-item label="联系人姓名" prop="contactUserRealName">
                    <el-input v-model="formData.contactUserRealName" placeholder="请输入联系人姓名"/>
                  </el-form-item>
                  <el-form-item label="区域编码" prop="areaCode">
                    <el-input v-model="formData.areaCode" placeholder="请输入区域编码"/>
                  </el-form-item>
                  <el-form-item label="问题类型" prop="qaType">
                    <el-select v-model="formData.qaType" placeholder="请选择问题类型">
                          <el-option label="请选择字典生成" value=""/>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="具体事项" prop="qaDetailType">
                    <el-select v-model="formData.qaDetailType" placeholder="请选择具体事项">
                          <el-option label="请选择字典生成" value=""/>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="提问方式" prop="qaMode">
                    <el-input v-model="formData.qaMode" placeholder="请输入提问方式"/>
                  </el-form-item>
                  <el-form-item label="首次内容缩写" prop="qaBgContentOmit">
                    <el-input v-model="formData.qaBgContentOmit" placeholder="请输入首次内容缩写"/>
                  </el-form-item>
                  <el-form-item label="用户昵称" prop="nickName">
                    <el-input v-model="formData.nickName" placeholder="请输入用户昵称"/>
                  </el-form-item>
                  <el-form-item label="用户手机号" prop="mobile">
                    <el-input v-model="formData.mobile" placeholder="请输入用户手机号"/>
                  </el-form-item>
                  <el-form-item label="问题标题" prop="qaTypeTitle">
                    <el-input v-model="formData.qaTypeTitle" placeholder="请输入问题标题"/>
                  </el-form-item>
                  <el-form-item label="具体事项" prop="qaDetailTypeTitle">
                    <el-input v-model="formData.qaDetailTypeTitle" placeholder="请输入具体事项"/>
                  </el-form-item>
                  <el-form-item label="省份" prop="provinceName">
                    <el-input v-model="formData.provinceName" placeholder="请输入省份"/>
                  </el-form-item>
                  <el-form-item label="市区" prop="areaName">
                    <el-input v-model="formData.areaName" placeholder="请输入市区"/>
                  </el-form-item>
                  <el-form-item label="对话内容">
                    <Editor v-model="formData.qaContent" height="150px"/>
                  </el-form-item>
                  <el-form-item label="关联线索ID" prop="adClueId">
                    <el-input v-model="formData.adClueId" placeholder="请输入关联线索ID"/>
                  </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
  import * as LegalAdviceClueQaApi from '@/api/ad/legaladviceclueqa'

  const {t} = useI18n() // 国际化
  const message = useMessage() // 消息弹窗

  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const formType = ref('') // 表单的类型：create - 新增；update - 修改
  const formData = ref({
                      id: undefined,
                      userId: undefined,
                      contactUserMobile: undefined,
                      contactUserRealName: undefined,
                      areaCode: undefined,
                      qaType: undefined,
                      qaDetailType: undefined,
                      qaMode: undefined,
                      qaBgContentOmit: undefined,
                      nickName: undefined,
                      mobile: undefined,
                      qaTypeTitle: undefined,
                      qaDetailTypeTitle: undefined,
                      provinceName: undefined,
                      areaName: undefined,
                      qaContent: undefined,
                      adClueId: undefined
  })
  const formRules = reactive({
                  adClueId: [{
              required: true, message: '关联线索ID不能为空', trigger: 'blur' }]
  })
  const formRef = ref() // 表单 Ref

  /** 打开弹窗 */
  const open = async (type: string, id?: number) => {
    dialogVisible.value = true
    dialogTitle.value = t('action.' + type)
    formType.value = type
    resetForm()
    // 修改时，设置数据
    if (id) {
      formLoading.value = true
      try {
        formData.value = await LegalAdviceClueQaApi.getLegalAdviceClueQa(id)
      } finally {
        formLoading.value = false
      }
    }
  }
  defineExpose({open}) // 提供 open 方法，用于打开弹窗

  /** 提交表单 */
  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {
    // 校验表单
    if (!formRef) return
    const valid = await formRef.value.validate()
    if (!valid) return
    // 提交请求
    formLoading.value = true
    try {
      const data = formData.value as unknown as LegalAdviceClueQaApi.LegalAdviceClueQaVO
      if (formType.value === 'create') {
        await LegalAdviceClueQaApi.createLegalAdviceClueQa(data)
        message.success(t('common.createSuccess'))
      } else {
        await LegalAdviceClueQaApi.updateLegalAdviceClueQa(data)
        message.success(t('common.updateSuccess'))
      }
      dialogVisible.value = false
      // 发送操作成功的事件
      emit('success')
    } finally {
      formLoading.value = false
    }
  }

  /** 重置表单 */
  const resetForm = () => {
    formData.value = {
                        id: undefined,
                        userId: undefined,
                        contactUserMobile: undefined,
                        contactUserRealName: undefined,
                        areaCode: undefined,
                        qaType: undefined,
                        qaDetailType: undefined,
                        qaMode: undefined,
                        qaBgContentOmit: undefined,
                        nickName: undefined,
                        mobile: undefined,
                        qaTypeTitle: undefined,
                        qaDetailTypeTitle: undefined,
                        provinceName: undefined,
                        areaName: undefined,
                        qaContent: undefined,
                        adClueId: undefined
    }
    formRef.value?.resetFields()
  }
</script>